const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastlist: [],
    srctype: "全部",
    customItem: '全部',
    array: [
      {'name':'全部',state:1},
      { 'name': '未提交',state:0},
      { 'name': '已审核', state: 100},      
      { 'name': '提交审核', state: 10 },
      { 'name': '初审通过', state: 20 },
      { 'name': '审批退回', state: 5 },
      { 'name': '商户申请', state: 30 },
      { 'name': '商户申请失败', state: 15 },
      { 'name': '门店失效', state: -10},
      ],
    index:0,
    key:'',
    saindex:0,
    saarray:[
      { 'name': '全部', 'value': 3 },
      { 'name': '营业活跃', 'value': 1 },
      {'name':'营业不活跃','value':0}
    ],
    puindex: 0,
    puarray: [
      { 'name': '全部', 'value': 3 },
      { 'name': '采购活跃', 'value': 1 },
      { 'name': '采购不活跃', 'value': 0 }
    ],
    model:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    var time = util.formatTime(new Date());
    time = time.split(' ')[0];
    time = time.split('-');
    var timearr = time[0] + '-' + time[1];
    
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight - 150,
          firstday: timearr + '-01',
          today: time[0] + '-' + time[1] + "-" + time[2],
          showfirst: timearr,
          endday: time[0] + '-' + time[1] + "-" + time[2],
        })
      },
    })

  //  var city = userinfo.ProName + "/" + userinfo.CityName + "/" + "全部";
    that.newsearch(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  searchstore: function () {
    this.setData({
      model:0
    })
    this.newsearch()
  },
  newsearch: function (isfirst = 0) {
    var that = this;
    var datas = {};
    datas.where = "CurrSMebID = '" + wx.getStorageSync('mebid')+"'";
    if (that.data.pustate && that.data.pustatess != 3){
      datas.where +=" and IsPUActive = 1";
    } else if (that.data.pustate==0){
      datas.where += " and IsPUActive = 0";
    }
    if (that.data.sastate && that.data.sastate!=3) {
      datas.where += " and IsSAActive = 1";
    } else if (that.data.sastate==0){
      datas.where += " and IsSAActive = 1";
    }
    if (that.data.key) {
      datas.where += " and AccName like '%"+that.data.key+"%'";
    }
    // datas.Begin = that.data.firstday;
    // datas.End = that.data.endday;
    // datas.Keyword = that.data.key ? "%" + that.data.key + "%" : "%%";
    // datas.PUActive = that.data.pustate ? that.data.pustate : 3;
    // datas.SAActive = that.data.sastate ? that.data.sastate : 3;
    // datas.MebID = wx.getStorageSync('mebid');
    //datas.MebID = "124e22579e9a422aa068b422a9193618"
    console.log(datas);
    var datacall = function (res) {
      console.log(res);
      var list = res.data.data;
      var satotal = 0;
      var sanum = 0;
      var putotal = 0;
      var punum = 0;
      var shopnum = list.length;
      for (var i = 0; i < list.length; i++) {
        list[i].SAMoney = post.toDecimal2(list[i].SASumMny);
        list[i].PUMoney = post.toDecimal2(list[i].PUSumMny);
        if (isfirst == 1) {
          satotal += list[i].SASumMny;
          sanum += list[i].SASumCount;
          putotal += list[i].PUSumMny;
          punum += list[i].PUSumCount;
        }
        if (list[i].AccName.length > 10) {
          list[i].AccNames = list[i].AccName.substring(0, 9) + "...";
        } else {
          list[i].AccNames = list[i].AccName;
        }
      }
      if (isfirst == 1) {
        var sato = satotal + '';
        sato = sato.split('.');
        if (sato[0].length > 5) {
          satotal = satotal / 10000;
          satotal = satotal.toFixed(2);
          satotal = satotal + '万元';
        } else {
          satotal = parseInt(satotal) + '元';
        }

        putotal = parseInt(putotal);
        that.setData({
          satotal: satotal,
          sanum: sanum,
          putotal: putotal,
          punum: punum,
          shopnum: shopnum,
          lastlist: res.data.data
        })
      } else {
        that.setData({
          lastlist: res.data.data
        })
      }

    }
    var res2 = post.getdata("/BO/PE_ShopStatus/Query", datas, datacall);
  },
  newsearch2: function (isfirst=0) {
    var that = this;
    var datas = {};
    datas.Begin = that.data.firstday;
    datas.End =that.data.endday;
    datas.Keyword = that.data.key ? "%" + that.data.key+"%":"%%";
    datas.PUActive = that.data.pustate ? that.data.pustate : 3;
    datas.SAActive = that.data.sastate ? that.data.sastate:3;
    var state = that.data.state;
    if(state){
      datas.ApplyState = that.data.state;
    }else{
      if(state==0){
        datas.ApplyState =0;
      }else{
        datas.ApplyState = 1;
      }
    }
    var pustate = that.data.pustate;
    if (pustate) {
      datas.PUActive = that.data.pustate;
    } else {
      if (pustate == 0) {
        datas.PUActive = 0;
      } else {
        datas.PUActive = 3;
      }
    }
    var sastate = that.data.sastate;
    if (sastate) {
      datas.SAActive = that.data.sastate;
    } else {
      if (sastate == 0) {
        datas.SAActive = 0;
      } else {
        datas.SAActive = 3;
      }
    }
    datas.MebID = wx.getStorageSync('mebid');
    //datas.MebID = "124e22579e9a422aa068b422a9193618"
    console.log(datas);
    var datacall = function (res) {
      console.log(res);
      var list =res.data.data;
      var satotal=0;
      var sanum=0;
      var putotal=0;
      var punum=0;
      var shopnum = list.length;
      for(var i=0;i<list.length;i++){
        list[i].SAMoney = post.toDecimal2(list[i].SAMoney);
        list[i].PUMoney = post.toDecimal2(list[i].PUMoney);
        if(isfirst==1){
          satotal += list[i].SAMoney;
          sanum += list[i].SACount;
          putotal += list[i].PUMoney;
          punum += list[i].PUCount;
        }
        if(list[i].AccName.length>10){
          list[i].AccNames = list[i].AccName.substring(0, 9) + "...";
        }else{
          list[i].AccNames = list[i].AccName;
        }
      }
      if(isfirst==1){
        var sato=satotal+'';
        sato = sato.split('.');
        if(sato[0].length>5){
          satotal=satotal/10000;
          satotal = satotal.toFixed(2);
          satotal=satotal+'万元';
        }else{
          satotal = parseInt(satotal)+'元';
        }
        
        putotal = parseInt(putotal);
        that.setData({
          satotal:satotal,
          sanum:sanum,
          putotal:putotal,
          punum:punum,
          shopnum:shopnum,
          lastlist: res.data.data
        })
      }else{
        that.setData({
          lastlist: res.data.data
        })
      }
     
    }
    var res2 = post.getdata("/BO/V_ServiceStore/Query", datas, datacall);
  },

  //开店申请状态选择
  bindPickerChange: function (e) {
    console.log(this.data.array[e.detail.value])
    this.setData({
      index: e.detail.value,
      srctype: this.data.array[e.detail.value].name,
      state: this.data.array[e.detail.value].state
    })
   // this.newsearch();
  },
  bindSAChange: function (e) {
    console.log(this.data.saarray[e.detail.value])
    this.setData({
      saindex: e.detail.value,
      satype: this.data.saarray[e.detail.value].name,
      sastate: this.data.saarray[e.detail.value].value
    })
   // this.newsearch();
  },
  bindPUChange: function (e) {
    console.log(this.data.puarray[e.detail.value])
    this.setData({
      puindex: e.detail.value,
      putype: this.data.puarray[e.detail.value].name,
      pustate: this.data.puarray[e.detail.value].value
    })
   // this.newsearch();

  },
  bindDateChange: function (e) {
    var that = this;
    var time = e.detail.value.split('-');
    var d = new Date(time[0], time[1], 0);
    that.setData({
      firstday: e.detail.value+"-1",
      endday: e.detail.value + "-"+d.getDate(),
      showfirst: e.detail.value
    })
  },
  tolinkman: function (e) {
    wx.navigateTo({
      url: '/pages/linkman/linkman?id=' + e.currentTarget.dataset.id,
    })
  },
  getkey: function (e) {
    this.setData({
      key: e.detail.value
    })
  },
  clearall: function () {
    var that = this;
    that.setData({
      key: ''
    });
    that.searchstore()
  },
  newdata:function(){

  },
  saoyisao: function () {
    var that = this;
    wx.scanCode({
      success: (res) => {
        console.log(res.result);
        var data = res.result.split('&');
        var mebarr = data[0].split(':');
        if (mebarr[0] == 'mebid' && mebarr[1] != '') {
          console.log(data)
          var datas = {};
          if (data[1]){
            var depid = data[1].split(':')[1];
          }else{
            var depid = '';
          }
          
          if(!depid){
            wx.showModal({
              title: '',
              content: '对方还未提交审核，无法添加',
            })
          }
          datas.where = "AccID = " + depid;
          var datacall = function (res) {
            console.log(res);
            if (res.data.data.length != 0) {
              if (res.data.data[0].ServiceMebID) {
                wx.showModal({
                  title: '',
                  content: '对方已经有客户经理，无法添加',
                })
                return;
              } else {
                console.log("oktian");
                that.addservice(depid);
              }
            }
          }
          var res2 = post.getdata("/BO/TR_Account/Query", datas, datacall);

        } else {
          wx.showModal({
            title: '',
            content: '不是符合规定的二维码',
          })
        }
      }
    })
  },
  //添加服务门店
  addservice: function (id) {
    var that = this;
    // var datas = {};
    var data = {};
    data.AccID = id;
    data.ServiceMebID = wx.getStorageSync('mebid');
    // datas.data = JSON.stringify(data);
    var datacall = function (res) {
      console.log(res);
      wx.showModal({
        title: '',
        content: '添加服务门店成功！',
      })
      var city = that.data.date;
      that.search(city)
    }
   // var res2 = post.getdata("/BO/TR_Account/Modify", data, datacall);// /Action/PK01/SetServiceMebID
    var res2 = post.getdata("/Action/PK01/SetServiceMebID", data, datacall);
  },
  closemodel:function(){
    this.setData({
      model:0
    })
  },
  showchoose:function(){
    this.setData({
      model:1
    })
  },
})